package com.softoa.serviceImp;

import com.softoa.mapper.DepartmentMapper;
import com.softoa.pojo.Department;
import com.softoa.pojo.DepartmentParam;
import com.softoa.pojo.User;
import com.softoa.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author LinKun
 * @date 2022-6-22 0:15
 */
@Service
public class DepartmentServiceImp implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public List<Department> departmentList() {
        return departmentMapper.departmentsList();
    }

    @Override
    public boolean departmentInsert(Department department) {
        department.setDelFlag("0");
        department.setStatus("1");
        int i = departmentMapper.sysDeptInsert(department);
        if (i <= 0) {
            return false;
        } else {
            return true;
        }

    }

    @Override
    public boolean departmentUpdate(DepartmentParam departmentParam) {
        Department department = new Department();
        department.setDeptId(departmentParam.getDeptId());
        department.setDeptName(departmentParam.getDeptName());
        department.setDepType(departmentParam.getDepType());
        department.setDepPhone(departmentParam.getDepPhone());
        department.setDepDate(departmentParam.getDepDate());
        department.setStatus(departmentParam.getStatus());
        department.setDelFlag(departmentParam.getDelFlag());
        department.setDepDescribe(departmentParam.getDepDescribe());
        int i = departmentMapper.departmentUpdate(department);
        if (i <= 0) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public java.util.List<User> showUsers(String deptId) {
        java.util.List<User> users = new LinkedList<>();
        int depId = Integer.parseInt(deptId);
        Department department = departmentMapper.departmentOneAndUser(depId);
        for (User ll : department.getUsers()) {
            users.add(ll);
        }
        return users;
    }

    @Override
    public boolean deleteOne(String deptId) {
        int depId = Integer.parseInt(deptId);
        int[] arr = {depId};
        int i = departmentMapper.departmentDeleteMultiple(arr);
        if (i <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteDepts(int[] ids) {
        int i = departmentMapper.departmentDeleteMultiple(ids);
        if (i <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public List<Department> departmentListMultiple(String deptName) {
        Department department=new Department();
        department.setDeptName(deptName);
        List<Department> departments = departmentMapper.departmentListMultiple(department);
        return departments;
    }
}
